package top.juanshen.print.escppos;

import com.alibaba.fastjson.JSONObject;
import org.springframework.util.ClassUtils;
import top.juanshen.print.util.ReadUtil;

import java.io.IOException;
import java.net.ConnectException;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Map;

/**
 * @author yanzhuan
 */
public class PrintJob implements Runnable{

	/**
	 * 任务类型：0=普通订单 1=预定单 2=支付订单 3=警告单 4=test单
	 */
	private int missionType;
	/**
	 * 需要打印的参数--可扩展
	 */
	private Map<String, Object> param;
	/**
	 * posIP地址
	 */
	private String ip;

	static final String BASEPATH = ClassUtils.getDefaultClassLoader().getResource("").getPath();

	
	public PrintJob(int missionType, Map<String, Object> param, String ip) {
		this.missionType = missionType;
		this.param = param;
		this.ip = ip;
	}

	public int getMissionType() {
		return missionType;
	}

	public void setMissionType(int missionType) {
		this.missionType = missionType;
	}

	public Map<String, Object> getParam() {
		return param;
	}

	public void setParam(Map<String, Object> param) {
		this.param = param;
	}

	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}


	@Override
	public void run() {
			while(true){
				try {
					PrintJob printJob = PrintQueue.take();
					EscPos pos = printer(printJob.getIp());
					System.out.println("begin print job...");
					pos.print(jsonTemplateByMissionType(printJob.getMissionType()), JSONObject.toJSONString(printJob.getParam()));
					System.out.println("end print job...");
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				catch (IOException e) {
					e.printStackTrace();
				}
			}
	}

	private static EscPos printer(String ip){
		EscPos pos = null;
		try {
			System.out.println("get the printer pos...");
			pos = EscPos.getInstance(ip);
		} catch (ConnectException e) {
			System.out.println("the printer have not connect... please check the power is on");
		} catch (UnknownHostException ne) {
			System.out.println("the printer's IP address is not found...");
		} catch (SocketException e){
			System.out.println("Internet is broken...");
		}
		catch (IOException e) {
			e.printStackTrace();
			System.out.println("printer error" + e);
		}
		return pos;
	}



	private static String jsonTemplateByMissionType(int missionType) throws IOException{
		String template = "";
		String path =  BASEPATH+"static/escpos/";
		String pix = ".json";
		switch (missionType) {
			case 0:
				template = path + "JsonTemplate" + pix;
				break;
			case 1:
				template = path + "localhost" + pix;
				break;
			case 2:
				template = path + "pay" + pix;
				break;
			case 3:
				template = path + "warn" + pix;
				break;
			case 4:
				template = path + "test" + pix;
				break;

			default:
				template = path + "JsonTemplate" + pix;
				break;
		}
		String json = readTxt(template, "utf-8");

		return json;
	}

	private static String readTxt(String filePathAndName, String encoding)
			throws IOException {
		return ReadUtil.ReadText(filePathAndName,encoding);
	}

}
